package com.cacf.uddi.clientExample;

import inspireit.uddi.base.BusinessKey;
import inspireit.uddi.base.Name;
import inspireit.uddi.base.UDDIConstants;
import inspireit.uddi.base.UDDIException;
import inspireit.uddi.collections.Names;
import inspireit.uddi.collections.ServiceInfos;
import inspireit.uddi.request.UDDIProfile;
import inspireit.uddi.request.UDDIQuerier;
import inspireit.uddi.response.ServiceList;
import inspireit.uddi.response.base.ServiceInfo;

import java.net.URL;

/**
 * This example shows how to find services.
 * 
 * @author Bertrand Fontaine, <a href="http://www.inspireit.biz"
 *         target="INSPIREIT">INSPIRE IT</a>
 */
public class FindServiceExample {

	public FindServiceExample(String queryURL, String publishURL, String securityURL, String username, String password)throws Exception {
		
		System.out.println("Find");
		
		// first ensure that we have a saved BusinessEntity
		SaveServiceExample saveService = new SaveServiceExample(queryURL, publishURL, securityURL, username, password);
		
		BusinessKey businessKey = saveService.getBusinessKey();
		
		// first define the appropriate UDDI profile
		UDDIProfile profile = new UDDIProfile();
		profile.setQueryURL(new URL(queryURL));

		// uncomment one of the following lines to use UDDI V2 or V3 messaging
		// instead of the default V1
		// profile.setVersion(UDDIConstants.UDDI_V2);
		 profile.setVersion(UDDIConstants.UDDI_V3);

		UDDIQuerier querier = UDDIQuerier.getQuerier(profile);

		try {
			ServiceList serviceList = querier.findService(businessKey, new Name("%INSPIRE%"));
			
			if (serviceList == null) {
				System.out.println("Not found.");
			} else {
				ServiceInfos serviceInfos = serviceList.getServiceInfos();
				ServiceInfo serviceInfo[] = serviceInfos.toArray();

				for (int i = 0; i < serviceInfo.length; i++) {
					ServiceInfo si = serviceInfo[i];
					Names names = si.getNames();
					Name name[] = names.toArray();
					System.out.println("ServiceInfo #" + i + ": " + name[0]);
				}
			}
		} catch (UDDIException e) {
			e.printStackTrace();
		}
	}

	public static void main(String args[]) throws Exception {
//		if ((args.length != 4) && (args.length != 5)) {
//			System.out.println("FindServiceExample usage: java FindServiceExample <queryURL> <publishURL> [<securityURL>] <username> <password>");
//			System.exit(0);
//		}

		String queryURL    = "http://localhost:8080/juddiv3/services/inquiry"; 
        String publishURL  = "http://localhost:8080/juddiv3/services/publish"; 
        String securityURL = "http://localhost:8080/juddiv3/services/security"; 
        String username    = "root"; 
        String password    = "root";

//		if (args.length == 4) {
//			username = args[2];
//			password = args[3];
//		} else { // must be 5 ...
//			securityURL = args[2];
//			username = args[3];
//			password = args[4];
//		}

		new FindServiceExample(queryURL, publishURL, securityURL, username, password);
		return;
	}
}